package com.open.launcher.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;

import com.open.launcher.utils.Consts;
import com.open.launcher.utils.Paths;
import com.open.library.utils.PreferencesUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.Timer;
import java.util.TimerTask;

import okhttp3.Call;

/**
 * Created by legend on 18-5-30.
 */

public class HeartServer extends Service {
    private static String TAG = "HeartServer";
    private Timer timer;
    //任务
    private TimerTask task;
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        task = new TimerTask() {
            public void run() {
                OkHttpUtils.post().url("http://"+PreferencesUtils.getString(getApplicationContext(), Consts.IP_ADDR_KEY)+ Paths.AFTER_HEART)
                        .addParams("room_number", PreferencesUtils.getString(getApplicationContext(), Consts.IP_ROOM_NO_KEY))
                        .build()
                        .execute(new StringCallback() {
                            @Override
                            public void onError(Call call, Exception e, int id) {
                                Log.i(TAG, "onError: ");
                            }

                            @Override
                            public void onResponse(String response, int id) {
                                Log.i(TAG, "onResponse: "+response);
//                                HeartBean bean = JSON.parseObject(response,HeartBean.class);
//                                if (bean.getCode()==1){
//                                    Log.i(TAG, "onResponse: "+bean.getMessage());
//                                }else {
//                                    Log.i(TAG, "onResponse: "+bean.getMessage());
//                                    Toast.makeText(getApplicationContext(),"网络异常",Toast.LENGTH_SHORT).show();
//                                }
                            }
                        });
            }
        };
        timer = new Timer(true);
        timer.schedule(task, 0, 60*1000);
    }
}
